package com.axiang.module_user.viewmodel;

import android.app.Application;
import androidx.databinding.ObservableField;
import androidx.annotation.NonNull;

import com.axiang.lib_api.bus.RxBus;
import com.axiang.lib_api.bus.RxSubscriptions;
import com.axiang.lib_common.binding.command.BindingAction;
import com.axiang.lib_common.binding.command.BindingCommand;
import com.axiang.lib_common.mvvm.viewmodel.BaseViewModel;
import com.ren.lib_path.router.RouterActivityPath;
import com.axiang.module_user.model.UserModel;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class UserViewModel extends BaseViewModel<UserModel> {
    Disposable disposable;

    public ObservableField<String> username = new ObservableField<>("登录");

    public UserViewModel(@NonNull Application application, UserModel model) {
        super(application, model);
    }

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startRouter(RouterActivityPath.Sign.PAGER_LOGIN);

            disposable =  RxBus.getDefault().toObservable(String.class).subscribe(new Consumer<String>() {
                @Override
                public void accept(String s) throws Exception {

                    username.set(s);

                    RxSubscriptions.remove(disposable);
                }
            });

            RxSubscriptions.add(disposable);
        }
    });
}
